<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class LinkController extends Controller
{
    //添加链接
    public function add()
    {
    	$title = '友情链接添加';
    	return view('admin.link.add',['title' => $title]);
    }

    //执行添加链接页面
    public function insert(Request $request)
    {
    	$data = $request -> except('_token');
    	// dd($data);

    	//设置限制条件
    	$this -> validate($request, [
    		            	'name' => 'required|max:90',
    		             	 'url' => 'required',
    			         'content' => 'required',
    		],[
    	 		   'name.required' => '友情链接网站名称不能为空',
    	   		    'name.max' => '友情链接网站名称不能超过30位',
    			    'url.required' => '友情链接网站地址不能为空',
    			'content.required' => '友情链接网站描述不能为空',

    		]);

    	 //处理pic
        if($request -> hasFile('logo'))
        {
            if($request -> file('logo') -> isValid())
            {
                //获取后缀名
                $suffix = $request -> file('logo') -> getClientOriginalExtension();
                // echo $suffix;
                //随机文件名
                $fileName = time().mt_rand(100000, 999999).'.'.$suffix;
                // $path = date('Ymd');
                $path = './uploads';
                $request -> file('logo') -> move($path,$fileName);

                $data['logo'] = trim($path.'/'.$fileName,'.');
                // echo $data['pic'];    
            }
        }

    	//执行添加
    	$res = DB::table('link') -> insert($data);
    	//判断
		if($res)
		{
			return redirect('/admin/link/index') -> with('success', '添加成功');
		}else
		{
			return back() -> with('error', '添加失败');
		}
    }

    //列表页
    public function index(Request $request)
    {
    	$title = '友情链接列表';
    	//查询数据
        $data = DB::table('link') -> where(function($query) use ($request)
        {
                           $query -> where('name','like','%'.$request 
                                  -> input('keywords').'%');
        }) 
                                  -> paginate($request -> input('num',10));
		// dd($data);
        return view('admin.link.index',['title' => $title,'data' => $data,'request' => $request -> all()]);
    }


    //编辑页面
    public function edit($id)
    {
    	$title = '友情链接编辑';
        $data = DB::table('link')
            -> where('id','=',$id) 
            -> first();
        //解析模板
        return view('admin.link.edit',['title' => $title,'data' => $data]);

    }

    //执行更新页面
    public function update(Request $request)
    {
    	$data = $request -> except('_token','id','oldlogo');
    	// dd($data);
    	$id = $request -> input('id');
    	// dd($id);

    	//设置限制条件
    	$this -> validate($request, [
    			            'name' => 'required|max:90',
    		             	 'url' => 'required',
    			         'content' => 'required',
    		],[
    			   'name.required' => '友情链接网站名称不能为空',
    			    'username.max' => '友情链接网站名称不能超过30个汉字',
    			    'url.required' => '友情链接网站地址不能为空',
    			'content.required' => '友情链接网站描述不能为空',

    		]);

    	//获取老图片地址
        $oldpath = '.'.$request -> input('oldlogo');

    	 //处理pic
        if($request -> hasFile('logo'))
        {
            if($request -> file('logo') -> isValid())
            {
                //获取后缀名
                $suffix = $request 
                    -> file('logo') 
                    -> getClientOriginalExtension();
                // echo $suffix;
                //随机文件名
                $fileName = time().mt_rand(100000,999999).'.'.$suffix;
                // $path = date('Ymd');
                $path = './uploads';
                $request -> file('logo') -> move($path,$fileName);

                $data['logo'] = trim($path.'/'.$fileName,'.');
                // echo $data['pic']; 

                //判断有老图片删除
                if(file_exists($oldpath))
                {
                    unlink($oldpath);
                }   
            }
        }

        //执行添加数据
        $res = DB::table('link') 
            -> where('id',$id) 
            -> update($data);

        //判断
        if($res)
        {
            return redirect('/admin/link/index') -> with(['success' => '更新成功']);
        }else
        {
            return back() -> with(['error' => '更新失败']);
        }
    }

    //执行删除
    public function delete($id)
    {
    	$res = DB::table('link') 
            -> where('id',$id) 
            -> delete();
    	if($res)
        {
            return redirect('/admin/link/index') -> with(['success' => '删除成功']);
        }else
        {
            return back() -> with(['error' => '删除失败']);
        }
    }
}
